package com.tegtech.starter.security.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import com.tegtech.common.domain.Result;
import com.tegtech.common.utils.http.ServletUtils;
import com.tegtech.starter.security.domain.LoginUser;
import com.tegtech.starter.security.service.TokenService;

/**
 * 登出成功
 * @Author Jun
 * @date 2020年5月30日 下午8:35:52
 */
@Component
public class OpenLogoutSuccessHandler implements LogoutSuccessHandler {

	@Autowired
	private TokenService tokenService;
	
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //没有logout不走这里、若使用security的formLogin则自己添加业务实现（移除token等等）
    	LoginUser loginUser = tokenService.getLoginUser(request);
		if (loginUser != null) {
			// 删除用户缓存记录
			tokenService.delLoginUser(loginUser.getToken());
		}
		ServletUtils.reponseData(response, Result.ok());
    }

}